var a = getApp(), t = require("../../../mybehaviors/location/location");

Page({
    behaviors: [ t ],
    data: {
        banner: [],
        notice: "",
        category: [],
        goods: [],
        brand: [],
        advertising: {},
        recommandList: [],
        purchaseTime: 108e6,
        Hei: "",
        page: 1,
        totalPage: 0,
        isLoadMore: !0,
        cakeCity: ""
    },
    onLoad: function(a) {
        a.city && this.setData({
            cakeCity: a.city.trim()
        }), this.goodsByRecommand(), this.frontendIndex();
    },
    imgH: function(a) {
        var t = wx.getSystemInfoSync().windowWidth * a.detail.height / a.detail.width + "px";
        this.setData({
            Hei: t
        });
    },
    addressTo: function() {
        wx.navigateTo({
            url: "/packageI/citypickerPage/citypickerPage?plu=cake"
        });
    },
    swiperNav: function(a) {
        var t = a.target.dataset.url;
        wx.navigateTo({
            url: t
        });
    },
    handpickNav: function(a) {
        var t = a.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/packageF/sachertorte/classify/classify?brandId=".concat(t, "&city=").concat(this.data.cakeCity)
        });
    },
    classifyNav: function(a) {
        var t = a.currentTarget.dataset.id || "", e = a.currentTarget.dataset.name;
        wx.navigateTo({
            url: "/packageF/sachertorte/classify/classify?id=".concat(t, "&classifyName=").concat(e, "&city=").concat(this.data.cakeCity)
        });
    },
    searchNav: function() {
        wx.navigateTo({
            url: "/packageF/sachertorte/sachertorteSearch/sachertorteSearch?city=".concat(this.data.cakeCity)
        });
    },
    purchaseOn: function() {
        wx.navigateTo({
            url: "/packageF/sachertorte/purchase/purchase?city=".concat(this.data.cakeCity)
        });
    },
    goodsNav: function(a) {
        var t = a.currentTarget.dataset.id || "";
        wx.navigateTo({
            url: "/packageF/sachertorte/sachertorteGoods/sachertorteGoods?id=".concat(t, "&city=").concat(this.data.cakeCity)
        });
    },
    adverNav: function(a) {
        var t = a.target.dataset.url;
        wx.navigateTo({
            url: t
        });
    },
    onReady: function() {
        var a = this;
        try {
            this._getLocation(function(t, e) {
                var o = t.address_component.city || "";
                a.data.cakeCity || a.setData({
                    cakeCity: o
                }), a.goodsByRecommand(), a.frontendIndex();
            });
        } catch (a) {
            console.log(a), this.goodsByRecommand(), this.frontendIndex();
        }
    },
    frontendIndex: function() {
        var t = this, e = {
            city: this.data.cakeCity
        }, o = a.getNetAddresss("plugin.yz-supply-cake.frontend.index.index");
        a._getNetWork({
            url: o,
            data: e,
            success: function(a) {
                var e = a.data;
                if (1 == e.result) {
                    var o, n = e.data.goods, i = new Date(null === (o = n[0]) || void 0 === o ? void 0 : o.promotion_end_at).getTime(), s = new Date().getTime();
                    t.setData({
                        advertising: e.data.advertising,
                        goods: e.data.goods,
                        category: e.data.category,
                        banner: e.data.banner,
                        brand: e.data.brand,
                        notice: e.data.notice,
                        purchaseTime: i - s
                    }), console.log(t.data.goods);
                } else wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(a) {
                console.log(a);
            }
        });
    },
    goodsByRecommand: function() {
        var t = this, e = {
            page: this.data.page,
            city: this.data.cakeCity
        }, o = a.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getGoodsByRecommand");
        a._postNetWork({
            url: o,
            data: e,
            success: function(a) {
                var e = a.data;
                wx.hideLoading(), wx.hideNavigationBarLoading(), wx.stopPullDownRefresh(), 1 == e.result ? (t.data.totalPage = e.data.last_page, 
                t.data.totalPage || (t.data.totalPage = 0), t.setData({
                    recommandList: e.data.data
                })) : (t.data.page = t.data.page - 1, t.isLoadMore = !1, wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                }));
            },
            fail: function(a) {
                console.log(a);
            }
        });
    },
    getMoreData: function() {
        var t = this;
        if (this.data.isLoadMore = !1, !(this.data.page >= this.data.totalPage)) {
            this.data.page = this.data.page + 1;
            var e = {
                page: this.data.page,
                city: this.data.cakeCity
            }, o = a.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getGoodsByRecommand");
            a._getNetWork({
                url: o,
                data: e,
                success: function(a) {
                    var e = a.data, o = e.data.data;
                    1 == e.result ? (t.data.isLoadMore = !0, t.setData({
                        recommandList: t.data.recommandList.concat(o)
                    })) : (t.data.query.page = t.data.query.page - 1, t.data.isLoadMore = !1, wx.showToast({
                        icon: "none",
                        title: e.msg,
                        duration: 1500
                    }));
                },
                fail: function(a) {
                    console.log(a);
                }
            });
        }
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        this.data.isLoadMore ? this.getMoreData() : console.log("没有更多了");
    },
    onShareAppMessage: function() {}
});